#include "core/profile/default_profile.h"
#include "core/auth/shahmac1_signer.h"
#include "core/regions/end_point_provider.h"

namespace aliyun {

DefaultProfile* DefaultProfile::GetProfile(const string& region_id, const string& access_key_id, const string& access_secret) {
  return new DefaultProfile(region_id, Credential(access_key_id, access_secret));
}

void DefaultProfile::ReleaseProfile(DefaultProfile *default_profile) {
  if(NULL != default_profile) {
    delete default_profile;
    default_profile = NULL;
  }
}

DefaultProfile::DefaultProfile(const string& region_id, const Credential& credential)
  : region_id_(region_id), credential_(credential), i_signer_(NULL) {
}

DefaultProfile::~DefaultProfile() {
}

ISigner* DefaultProfile::GetSigner(void) {
  if(NULL == i_signer_) {
    i_signer_ = new ShaHmac1Signer();
  }
  return i_signer_;
}

const string& DefaultProfile::GetRegionId(void) {
  return region_id_;
}

const string& DefaultProfile::GetFormat(void) {
  return accept_format_;
}

Credential& DefaultProfile::GetCredential(void) {
  return credential_;
}

std::vector<Endpoint>& DefaultProfile::GetEndpoints(void) {
  return EndpointProvider::GetEndpoints();
}

void DefaultProfile::AddEndpoint(const string& endpoint_name,
  const string& region_id, const string& product, const string& domain) {
  Endpoint* endpoint = FindEndpointByName(endpoint_name);
  if(NULL == endpoint) {
    std::vector<Endpoint>& endpoints = GetEndpoints();
    std::vector<string> region_ids;
    region_ids.push_back(region_id);
    std::vector<ProductDomain> product_domains;
    product_domains.push_back(ProductDomain(product, domain));
    endpoints.push_back(Endpoint(endpoint_name, region_ids, product_domains));
  } else {
    std::vector<string> region_ids = endpoint->GetRegionIds();
    std::vector<string>::const_iterator iter_region_id;
    for(iter_region_id = region_ids.begin(); iter_region_id != region_ids.end(); iter_region_id++) {
      if(*iter_region_id == region_id) {
        break;
      }
    }
    if(iter_region_id == region_ids.end()) {
      region_ids.push_back(region_id);
      endpoint->SetRegionIds(region_ids);
    }
    std::vector<ProductDomain> product_domains = endpoint->GetProductDomains();
    std::vector<ProductDomain>::const_iterator iter_product_domain;
    for(iter_product_domain = product_domains.begin(); iter_product_domain != product_domains.end(); iter_product_domain++) {
      if(iter_product_domain->GetProductName() == product && iter_product_domain->GetDomainName() == domain) {
        break;
      }
    }
    if(iter_product_domain == product_domains.end()) {
      product_domains.push_back(ProductDomain(product, domain));
      endpoint->SetProductDomains(product_domains);
    }
  }
}

Endpoint* DefaultProfile::FindEndpointByName(const string& endpoint_name) {
  std::vector<Endpoint>& endpoints = GetEndpoints();
  std::vector<Endpoint>::iterator iter;
  for(iter = endpoints.begin(); iter != endpoints.end(); iter++) {
    if(iter->GetName() == endpoint_name) {
      return &(*iter);
    }
  }
  return NULL;
}


} // namespace aliyun {

// end of file
